import {Component, ElementRef, OnInit} from '@angular/core';
import {Subscription} from '../../../../../node_modules/rxjs';
import {IndexService} from '../../services/index.service';
import {SocketService} from '../../services/socket.service';
import {TipService} from '../../services/tip.service';
import {ExtendHttpService} from '../../../common/extend-http.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-cash-desk',
  templateUrl: './cash-desk.component.html',
  styleUrls: ['./cash-desk.component.css']
})
export class CashDeskComponent implements OnInit {

  // 餐桌列表
  tableList = [];
  tableFlag = false;
  tipType = 'error';
  rightSpinnerState = false;

  unMsMessage: Subscription;

  // 餐桌滚动条状态
  scrollTop: number = 0;

  // 餐桌状态说明：0(空闲) 1(使用) 2(预约)
  constructor(private indexService: IndexService,
              private extendHttp: ExtendHttpService,
              private router: Router,
              private socketService: SocketService,
              private tipService: TipService,
              private elementRef: ElementRef,
              private activatedRoute: ActivatedRoute
  ) {
    this.activatedRoute.queryParams.subscribe(param => {
      this.scrollTop = param['scrollTop'];
      if (param['loginFlag']) {
        console.log('loginFlag');
        this.indexService.emptyData();
      }
    });

  }

  getTableList() {
    this.extendHttp.post('/merchantApp/singleprocess/getAlltableList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tableList = data.list;
        if (!data.list.length) {
          this.tableFlag = true;
        }
        if (this.scrollTop) {
          // $(document).scrollTop(this.scrollTop)
          $('html,body').animate({scrollTop: this.scrollTop}, 0.1);
        }
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  ngOnInit() {

    $('app-table-list').on('click', () => {
      this.rightSpinnerState = false;
    });
    let first = null;
    cBack = () => {
      if (!first) {
        first = new Date().getTime();
        setTimeout(() => {
          first = null;
        }, 1000);

      } else {
        if (new Date().getTime() - first < 1000) {
          webview.close();
        }
      }
    };
    this.getTableList();

    this.unMsMessage = this.socketService.wsMessage.subscribe(data => {
      if (data.code == '1003') {
        this.getTableList();
      }
    });
  }

  // 桌子信息
  tableInfo(table) {
    this.scrollTop = $(document).scrollTop();
    if (table.state == '0') { // 空闲
      // let data = this.indexService.getData(), countNum = 0;
      // data.forEach(v => {
      //   countNum += v.count;
      // });
      // if (countNum) {
      //   this.router.navigate(['/main/order/order-submit'], {queryParams: {'of.tid': table.id, scrollTop: this.scrollTop}});
      // }

    } else if (table.state == '1' || table.state == '3') {  // 使用
      if (location.href.indexOf('mark=choose-table') !== -1) return;
      this.router.navigate(['/main/operation/desk-pay'], {
        queryParams: {
          'tid': table.id,
          scrollTop: this.scrollTop
        }
      });
    } else if (table.state == '2') { // 预约
      // let data = this.indexService.getData(), countNum = 0;
      // data.forEach(v => {
      //   countNum += v.count;
      // });
      //
      // // 查看当前桌有预约信息，购物车没有数据时跳转
      // if (countNum === 0) {
      //   this.router.navigate(['/main/table/table-reserve'], {queryParams: {'of.tid': table.id, state: 0, scrollTop: this.scrollTop}});
      // } else {
      //   // 查看当前桌有预约信息，购物车有数据时跳转
      //   this.router.navigate(['/main/order/order-submit'], {
      //     queryParams: {
      //       'of.tid': table.id,
      //       tableState: table.state,
      //       scrollTop: this.scrollTop
      //     }
      //   });
      // }
    }
  }

  // 右上角下拉列表
  changeRightSpinnerState(e) {
    e.preventDefault();
    e.stopPropagation();
    this.rightSpinnerState = !this.rightSpinnerState;
  }

  ngOnDestroy() {
    this.unMsMessage.unsubscribe();
  }

}
